﻿<template>
  <el-dialog
    v-drag-dialog
    :title="dlgTitle"
    :visible.sync="dialogFormVisible"
    ref="dlg"
    width="800px"
    :close-on-click-modal="false"
  >
    <el-form
      :rules="rules"
      ref="dataForm"
      :model="temp"
      label-position="right"
      label-width="130px"
      size="small"
      style="width: 80%; margin-left:6px;"
    >
    
      <el-form-item label="图书序号：" prop="BookID" v-if="temp.BookID>0">
        <el-input placeholder="图书序号" v-model="temp.BookID" :disabled="temp.BookID>0"></el-input>
      </el-form-item>
      <el-form-item label="图书号：" prop="BookNo">
        <el-input placeholder="图书号" v-model="temp.BookNo" ></el-input>
      </el-form-item>
      <el-form-item label="图书名：" prop="BookName">
        <el-input placeholder="图书名" v-model="temp.BookName" ></el-input>
      </el-form-item>
      <el-form-item label="作者：" prop="BookAuthor">
        <el-input placeholder="作者" v-model="temp.BookAuthor" ></el-input>
      </el-form-item>
      <el-form-item label="出版社：" prop="BookPublisher">
        <el-input placeholder="出版社" v-model="temp.BookPublisher" ></el-input>
      </el-form-item>
      <el-form-item label="页码数：" prop="BookPages">
        <el-input-number placeholder="页码数" :precision="0" v-model="temp.BookPages" ></el-input-number>
      </el-form-item>
      <el-form-item label="价格：" prop="BookPrice">
        <el-input-number placeholder="价格" :precision="2" v-model="temp.BookPrice" ></el-input-number>
      </el-form-item>
      <el-form-item label="图书类别：" prop="BookKind">
        <el-select class="filter-item" v-model="temp.BookKind" width="100%">
          <el-option
            v-for="item in bookSexOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出版日期：" prop="BookPublishdate">
        <!-- <el-input placeholder="出版日期" v-model="temp.BookPublishdate" ></el-input> -->
            <el-date-picker
          v-model="temp.BookPublishdate"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="上传图片：">
        <el-upload
          class="avatar-uploader"
          :action="uploadURL"
          :show-file-list="false"
          :on-success="handleUploadSuccess">
          <img v-if="temp.BookImage" :src="imageURL" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-input disabled v-model="temp.BookImage"></el-input>
      </el-form-item>
      
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="Cancel">取消</el-button>
      <el-button type="primary" @click="Save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { saveBookInfo, getBookInfo } from '@/api/book';
export default {
  components: {
    
  },
  data() {
    let checkMobile = (rule, value, callback) => {
      if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'));
      } else callback();
    };
    return {
      dlgTitle: '',
      dialogFormVisible: false,
      baseURL: process.env.BASE_API,
      imageURL:'',
      uploadURL: process.env.BASE_API + '/Book/UploadFile', // api 的 base_url
      temp: {
        BookID: 0,
        BookImage:'',
      },
      rules: {
        BookNo: [
          { required: true, message: '必填字段1', trigger: 'blur' }
        ],
        BookName: [
          { required: true, message: '必填字段2', trigger: 'blur' }
        ],
        BookAuthor: [
          { required: true, message: '必填字段3', trigger: 'blur' }
        ],
        BookPublisher: [
          { required: true, message: '必填字段4', trigger: 'blur' }
        ],
       
      },
      bookStateOptions: [
        {
          value: 1,
          label: "在读"
        },
        {
          value: 0,
          label: "删除"
        }
      ],
      bookSexOptions: [
        {
          value: "专业书",
          label: "专业书"
        },
        {
          value: "工具书",
          label: "工具书"
        },
        {
          value: "手册",
          label: "手册"
        }
      ],
      
    };
  },
  methods: {
    AddDialog(data) {
      let row = data || null;
      this.initInfo();
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
      this.dlgTitle = '添加图书';
    },
    EditDialog(id) {
      this.initInfo();
      this.dialogFormVisible = true;
      this.temp.BookID = id;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
      this.dlgTitle = '修改图书';
      this.getRecordInfo(this.temp.BookID);
    },
    initInfo() {
      this.temp.BookID = 0;
      this.temp.BookImage = '';
      
    },
    getRecordInfo(id) {
      getBookInfo(id).then(rsp => {
        if (rsp.code === 0) {
          let record = rsp.data;
          this.temp = record;
          this.imageURL = this.baseURL + this.temp.BookImage;
        } else {
          this.dialogFormVisible = false;
          this.$message.error(rsp.msg);
        }
      });
    },
    Save() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm('确认保存？', '提示', {
            type: 'warning',
            callback: action => {
              if (action == 'confirm') {
                saveBookInfo(this.temp).then(rsp => {
                  if (rsp.code === 0) {
                    this.dialogFormVisible = false;
                    this.$message.success(this.$refs.dlg.title + '成功!');
                    this.$emit('onRefresh');
                  } else {
                    this.$message.error(rsp.msg);
                  }
                });
              }
            }
          });
        }
      });
    },
    Cancel() {
      this.dialogFormVisible = false;
    },
    handleUploadSuccess(res, file) {
  this.temp.BookImage = res.data.url
  this.imageURL = this.baseURL + this.temp.BookImage
    },
  }
};
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

